package com.shapawan.tourism.util;

import com.shapawan.tourism.entity.AdminUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {
    
    /**
     * 获取当前登录用户
     * @return 当前登录的AdminUser对象，如果未登录则返回null
     */
    public static AdminUser getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated() && authentication.getPrincipal() instanceof AdminUser) {
            return (AdminUser) authentication.getPrincipal();
        }
        return null;
    }
    
    /**
     * 获取当前登录用户名
     * @return 当前登录的用户名，如果未登录则返回null
     */
    public static String getCurrentUsername() {
        AdminUser user = getCurrentUser();
        return user != null ? user.getUsername() : null;
    }
} 